#include "StdAfx.h"
#include "BoxBuilder.h"
#include <BRepPrimAPI_MakeBox.hxx> 
#include "VarList.h"
#include "LabelTools.h"
#include "ShapeInfoFactory.h"

REGISTERE_SHAPEINFO(CBoxInfo, BOX_LWH_GUID);
//////////////////////////////////////////////////////////////////////////
CBoxInfo::CBoxInfo()
{
	m_szGUID = BOX_LWH_GUID;
	Reset();
}
CBoxInfo::~CBoxInfo()
{
}

void CBoxInfo::Reset()
{
	dLength = 0;
	dWidth = 0;
	dHeight = 0;
}
void CBoxInfo::GetParams(CVarList& varList, UINT nID)
{
	varList.SetID(BOX_LWH_GUID);
	varList.SetValue(START_POINT, ptStart);
	varList.SetValue(LENGTH, dLength);
	varList.SetValue(WIDTH, dWidth);
	varList.SetValue(HEIGHT, dHeight);
}

void CBoxInfo::SetParams(const CVarList& varList, UINT nID)
{
	CPointProxy point;
	varList.GetValue(START_POINT, point);
	ptStart = point;
	varList.GetValue(LENGTH, dLength);
	varList.GetValue(WIDTH, dWidth);
	varList.GetValue(HEIGHT, dHeight);
}

bool CBoxInfo::GetFromLabel(TDF_Label& label)
{
	TDF_Label childLabel;
	if(!LabelTools::GetChild(label, START_POINT, ptStart, childLabel))
		return false;

	if(!LabelTools::GetChild(label, LENGTH, dLength, childLabel))
		return false;

	if(!LabelTools::GetChild(label, WIDTH, dWidth, childLabel))
		return false;

	if(!LabelTools::GetChild(label, HEIGHT, dHeight, childLabel))
		return false;

	return true;
}

void CBoxInfo::AddToLabel(TDF_Label& label)
{
	LabelTools::SetChild(label, START_POINT, ptStart);
	LabelTools::SetChild(label, LENGTH, dLength);
	LabelTools::SetChild(label, WIDTH, dWidth);
	LabelTools::SetChild(label, HEIGHT, dHeight);
}

bool CBoxInfo::ModifyLabel(TDF_Label& label, TFunction_Logbook& log)
{
	if(!LabelTools::UpdateChild(label, START_POINT, ptStart, log))
		return false;

	if(!LabelTools::UpdateChild(label, LENGTH, dLength, log))
		return false;
	if(!LabelTools::UpdateChild(label, WIDTH, dWidth, log))
		return false;
	if(!LabelTools::UpdateChild(label, HEIGHT, dHeight, log))
		return false;

	return true;
}
bool CBoxInfo::IsModified(TDF_Label& label,const TFunction_Logbook& log)
{
	if(log.IsModified(label.FindChild(START_POINT)))
		return true; 

	if(log.IsModified(label.FindChild(LENGTH)))
		return true; 

	if(log.IsModified(label.FindChild(WIDTH)))
		return true; 

	if(log.IsModified(label.FindChild(HEIGHT)))
		return true; 

	return false;
}
//////////////////////////////////////////////////////////////////////////

CBoxBuilder::CBoxBuilder(void)
{
}

CBoxBuilder::~CBoxBuilder(void)
{
}
bool CBoxBuilder::Build(const CString& szCmdParam, TopoDS_Shape& shape)
{
	return false;
}

bool CBoxBuilder::Build(CBoxInfo* pShapeInfo, TopoDS_Shape& shape)
{
	try
	{
		BRepPrimAPI_MakeBox builder(pShapeInfo->ptStart, 
			pShapeInfo->dLength,
			pShapeInfo->dWidth,
			pShapeInfo->dHeight);

		shape = builder.Shape();
	}
	catch (...) 
	{
		return false;
	}

	return true;
}